﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WDTAsg2.DbBizObject;


namespace WDTAsg2.Member
{
    public partial class Products : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //First time loading
            if (!Page.IsPostBack)
            {


                //binding data to the gridview
                //lvProducts.DataSource = ProductInfo.GetAllProducts();
                //lvProducts.DataBind();

                //binding data to the first DropDownList
                dDListPCat.DataSource = CategoryInfo.GetParentCategories();
                dDListPCat.DataBind();

                dDListPCat.Items.Insert(0, new ListItem("All", "All"));
                dDListPCat.SelectedValue = "All";


                //dDListPCat.Items.Insert(0, new ListItem("andy", null));          
                //dDListPCat.Items.Insert(0, new ListItem("sam", null));
                //dDListPCat.SelectedValue = "sam";

            }
        }


        protected void dDListCat_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (dDListCat.SelectedValue == "All")
            {
                dDListPCat_SelectedIndexChanged(sender, e);
                return;
            }


            // Update grid view
            lvProducts.DataSource = ProductInfo.GetProductsByCategory(Convert.ToInt32(dDListCat.SelectedValue));
            lvProducts.DataBind();

            // Update model drop down
            dDListModel.DataSource = ProductInfo.GetProductsByCategory(Convert.ToInt32(dDListCat.SelectedValue));
            dDListModel.DataBind();

            dDListModel.Items.Insert(0, new ListItem("All", "All"));
            dDListModel.SelectedValue = "All";
            dDListModel.Visible = true;

        }


        protected void dDListModel_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (dDListModel.SelectedValue == "All")
            {
                dDListCat_SelectedIndexChanged(sender, e);
                return;
            }

            // Update grid view
            lvProducts.DataSource = ProductInfo.GetProductById(Convert.ToInt32(dDListModel.SelectedValue));
            lvProducts.DataBind();
        }



        protected void dDListPCat_SelectedIndexChanged(object sender, EventArgs e)
        {


            if (dDListPCat.SelectedValue == "All")
            {
                //binding data to the gridview
                lvProducts.DataSource = ProductInfo.GetAllProducts();
                lvProducts.DataBind();

                //binding data to the first DropDownList
                dDListPCat.DataSource = CategoryInfo.GetParentCategories();
                dDListPCat.DataBind();

                dDListPCat.Items.Insert(0, new ListItem("All", "All"));
                dDListPCat.SelectedValue = "All";

                // Hide drop down lists
                dDListCat.Visible = false;
                dDListModel.Visible = false;

                return;
            }

            // Update grid view
            lvProducts.DataSource = ProductInfo.GetProductsByCategory(Convert.ToInt32(dDListPCat.SelectedValue));
            lvProducts.DataBind();

            // Update model drop down
            dDListCat.DataSource = CategoryInfo.GetChildCategories(Convert.ToInt32(dDListPCat.SelectedValue));
            dDListCat.DataBind();

            dDListCat.Items.Insert(0, new ListItem("All", "All"));
            dDListCat.SelectedValue = "All";
            dDListCat.Visible = true;

            dDListModel.Visible = false;
        }


    }
}